package com.dream.myqiyi.image.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.dream.myqiyi.R;
import com.dream.myqiyi.image.ImageManager;
import com.dream.myqiyi.image.model.Bucket;
import com.dream.myqiyi.ui.NoteViewActivity;


/**
 * ͼƬ�б����
 * @author admin
 *
 */
public class AlbumListActivity extends Activity{

	private ListView bucktList;
	private AlbumListAdapter bucketListAdapter;
	private Button cancel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.image_album_list);
		 
		if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			Toast.makeText(getApplicationContext(), "SD Card Error", Toast.LENGTH_SHORT).show();
	        	return;
	        }
	        ImageManager.bucketList = ImageManager.loadAllBucketList(this);		

		 initUiData();
		 initListener();
	}
	
	public void initUiData(){		
		bucktList = (ListView)findViewById(R.id.bucket_list);
		bucketListAdapter = new AlbumListAdapter(this);
		bucktList.setAdapter(bucketListAdapter);
		cancel = (Button)findViewById(R.id.image_select_cancel);
	}
	
	public void initListener(){
		
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {				
				Intent intent = new Intent(AlbumListActivity.this, NoteViewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("select_result", "cancel");
				intent.putExtras(bundle);
				AlbumListActivity.this.setResult(NoteViewActivity.IMAGE_SELECTED_RESULT, intent);
				AlbumListActivity.this.finish();
			}
		});
		
		bucktList.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Bucket bucket = ImageManager.bucketList.get(position);
				Intent intent = new Intent();
				intent.setClass(AlbumListActivity.this, AlbumDetailActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("bucket", bucket);
				intent.putExtra("bundle", bundle);
				startActivityForResult(intent,0);
			}			
		});
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);  
        switch(resultCode) { //根据状态码，处理返回结果  
        case RESULT_OK:   
        	Bundle bundle =data.getExtras();   //获取intent里面的bundle对象  
        	String result = bundle.getString("select_image");
        	if (!result.equals("")) {
				Intent intent = new Intent(AlbumListActivity.this, NoteViewActivity.class);
				Bundle newBundle = new Bundle();
				newBundle.putString("select_result", "selected");
				newBundle.putString("image_file", result);
				intent.putExtras(newBundle);
				AlbumListActivity.this.setResult(NoteViewActivity.IMAGE_SELECTED_RESULT, intent);
				AlbumListActivity.this.finish();
        	}
        	break;     
         default:  
        	 break;  
         }


  }  

}
